package org.vectrics.common.struts;

import java.util.Locale;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.web.menu.Menu;
import org.vectrics.web.menu.MenuItem;

public abstract class MenuTag extends VectricsTagSupport {

	private static final long serialVersionUID = 1332334822470588267L;
	
	static private transient Logger log = Logger.getLogger(MenuTag.class);
    private String selectItemId = "";
    public static String TABLE_FONT = "subFont";
    private HttpSession session = null;
    private String contextPath = null;
    
    public MenuTag() {
    	super();
    }

	protected String getContextPath() {
		return(contextPath);
	}

	protected String getFormPath() {
		String formPath = (String)session.getAttribute("form-path");
		return(formPath);
	}	

	public String makeStartTag()  {
        log.debug("doRender() called");
		ServletRequest servletRequest = super.pageContext.getRequest();
		
        HttpServletRequest request = (HttpServletRequest)servletRequest;
		contextPath = request.getContextPath();
		session = request.getSession();
        
		String selectedMenuItemId = this.getSelected();     
		Menu menu = new Menu();
        buildMenu(menu);
		String menuContent = getMenuContent(menu, selectedMenuItemId, this.getRequest().getLocale());
        
		log.debug("doRender() - finished");
		return(menuContent);
    }
    
    
    public void setSelected(String itemId) {
    	this.selectItemId = itemId;
    }
    
	public String getSelected() {
		return(this.selectItemId);
	}

	
	protected abstract String getMenuId();        


    
	protected abstract void buildMenu(Menu menu);
	
	

	public String getMenuContent(Menu menu, String selectMenuItemId, Locale locale)  {
		log.debug("getMenuContent() called");
		String menuId = getMenuId();
		if ((selectMenuItemId != null) && (selectMenuItemId.trim().equals("") == false)) {
			MenuItem selectedItem = menu.findItemWithId(selectMenuItemId);
			if (selectedItem != null)
				menu.setSelectedItem(selectedItem);
			else {
				log.info("getMenuContent() - could not ind select item with id = " + selectMenuItemId + "  Menu ID = " + menuId);
			}
		}

		StringBuffer page = new StringBuffer();
		page.append("<script language=\"JavaScript\">");
		
		

		if (menu != null) {
			page.append("/* Menu Item JavaScript - Created by Vectrics Framework  */ \r\nvar AUTO_MENU_ITEMS_"
					+ menuId.toUpperCase() + " = [\n");
		if (menu.getChildList().size() > 0) {
			for (int i = 0; i < menu.getChildList().size(); i++) {
				MenuItem item = (MenuItem)menu.getChildList().elementAt(i);
				if (item.getVisible())
					page.append(item.getJavaScript(locale));
				}
		}
			page.append("];\n");
		}
		else {
			log.debug("getMenuContent() - no  menu definition found.  Request ignored.");
		}
		page.append("</script>");
		

		return(page.toString());
	}


    
}
